package com.seed.service.sysmanager;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Service;

import com.seed.commons.dbquery.PageModelForSQL;
import com.seed.commons.dbutil.DB;
import com.seed.commons.dbutil.DBBean;
import com.seed.commons.util.RandomUtil;
import com.seed.web.form.RoleForm;

@Service
public class RoleService implements IRole {

	private Log log = LogFactory.getLog(getClass()) ;
	
	@Override
	public boolean saveRole(RoleForm Role) {
		
		String sql = "INSERT INTO SEED_Role(RoleId,RoleName,roleDesc) VALUES(?,?,?)" ;
		
		Object[] param = new Object[] {RandomUtil.getRandomNum(5),Role.getRoleName(),Role.getRoleDesc()} ;
		
		try {
			DB.insert(sql,param) ;
			return true ;
		} catch (Exception e) {
			log.info("数据插入出错：" + e.getMessage());
			log.info("执行SQL：" + sql);
		}
		return false ;
	}

	@Override
	public void deleteRole(String id) {
		String sql = "DELETE FROM SEED_Role WHERE RoleCODE=?" ;
		
		try {
			DB.execute(sql, new Object[]{id}) ;
		} catch (Exception e) {
			log.info("数据删除出错：" + e.getMessage()) ;
			log.info("执行SQL：" + sql) ;
		}
		
	}

	@Override
	public void updateRole(RoleForm Role) {
		String sql = "UPDATE SEED_Role WHERE RoleCODE=? AND RoleNAME=?" ;
		
		try {
		} catch (Exception e) {
			log.info("数据修改出错：" + e.getMessage()) ;
			log.info("执行SQL：" + sql) ;
		}
	}

	@Override
	public DBBean queryRoleById(String id) {
		
		String sql = "SELECT RoleCODE,RoleNAME?" ;
		Object[] queryParams = new Object[]{id} ;

		try {
			return DB.getBeanFirst(sql, queryParams);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null ;
	}

	@Override
	public PageModelForSQL getScrollData(int currentPage, int lineSize) {
		String sql = "SELECT * FROM " +
					 "(" +
						 "SELECT " +
						 "ROWNUM rn,t1.* " +
						 "FROM (SELECT * from SEED_Role) t1 " +
						 "WHERE ROWNUM <= ?" +
					 ") " +
					 "WHERE rn > ? ORDER BY SORTDATE DESC" ;
		String count = "SELECT COUNT(*) c FROM SEED_Role" ;
		
		try {
			PageModelForSQL pm = new PageModelForSQL() ;
			pm.setResultList(DB.query(sql, new Object[]{lineSize,currentPage})) ;
			pm.setTotalRecord(Long.parseLong(DB.getBeanFirst(count, null).getbean("c").toString())) ;
			
			return pm ;
		} catch (Exception e) {
			log.info("分页查询出错：" + e.getMessage()) ;
		}
		return null;
	}

	@Override
	public List<DBBean> queryRole() {
		
		String sql = "SELECT * FROM SEED_Role" ;
		
		try {
			return DB.query(sql, null) ;
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return null;
	}

	@Override
	public boolean setRolesResource(int roleid, String[] menuid) {
		
		String sql = "INSERT INTO SEED_MENURESROLE(MENURESROLEID,RoleId,MLId) VALUES(?,?,?)" ;
		for (int i = 0; i < menuid.length; i++) {
			try {
				DB.insert(sql, new Object[]{RandomUtil.getRandomNum(5),roleid,menuid[i]}) ;
			} catch (Exception e) {
				log.info("权限资源填充发生错误：" + e.getMessage()) ;
				e.printStackTrace();
				return false ;
			}
		}
		return true;
	}

}
